<template>
	<view class="student-container">
		<cc-navbar></cc-navbar>
		<u-sticky>
			<view class="student-header">
				<cc-search :border="false" placeholder="姓名/手机号码搜索" :value.sync="query.student_name" @search="getList(1)"
					@clear="getList(1)"></cc-search>
				<cc-tabs :list="tabs" :current.sync="statusCurrent" @change="statusChange" />
			</view>
		</u-sticky>
		<view class="list" v-if="count">
			<view class="item" v-for="(row, index) in list" :key="index">
				<view class="cc-flex-col cc-p-x-32 cc-p-y-24">
					<view class="cc-flex cc-justify-between">
						<view class="cc-text-32 cc-text-600 u-line-1">
							{{ row.student_name }}
						</view>
						<u-tag shape="circle" plain plainFill size="mini" :type="row.statusData.type"
							:text="row.statusData.label"></u-tag>
					</view>
					<view class="cc-flex cc-items-center">
						<view class="cc-flex cc-items-center student-tag" v-if="row.contact_phone">
							<text>{{ row.contact_phone || '-' }}</text>
							<text v-if="row.contactRelationData.label">({{ row.contactRelationData.label }})</text>
						</view>
						<view v-if="row.sex" class="cc-flex cc-items-center student-tag">
							<view class="cc-m-r-6">{{ row.sexName }}</view>
							<u-icon :name="row.sex === 2 ? 'woman' : 'man'" color="#fff" size="32rpx"></u-icon>
						</view>
						<view v-if="row.age" class="student-tag">{{ row.age }}周岁</view>
					</view>
				</view>
				<view class="item-bottom">
					<u-row gutter="12" v-for="(ele, eindex) in action" :key="eindex"
						:customStyle="eindex !== 0 ? 'margin-top: 24rpx;' : ''">
						<u-col span="4" v-for="item in ele" :key="item.id">
							<u-button plain :icon="item.icon" size="small" type="info" :text="item.label"
								@click="handleRouter(item, row)"></u-button>
						</u-col>
					</u-row>
				</view>
			</view>
		</view>
		<cc-loadmore :showLimit="3" :loading="loading" :current="list.length" :count="count" />
		<cc-empty v-if="!loading && !count" mode="list" />
	</view>
</template>

<script>
	import {
		index as getStudent
	} from '@/codecat/api/education/student/index.js';
	import {
		getStudentStatus,
		getSex,
		intentionStudentStatus
	} from '@/codecat/config';
	export default {
		name: 'StudentIndex',
		data() {
			return {
				statusCurrent: 0,
				tabs: [],
				loading: false,
				list: [],
				count: 0,
				query: {
					status: null,
					student_name: '',
					page: 1,
					limit: 10
				},
				action: [
					[{
							id: '1',
							label: '上课记录',
							path: '/student-attend',
							icon: 'clock'
						},
						{
							id: '2',
							label: '报读课程',
							path: '/student-course',
							icon: 'order'
						},
						{
							id: '3',
							label: '订单记录',
							path: '/student-order',
							icon: 'list-dot'
						}
					],
					[{
							id: '4',
							label: '试听记录',
							path: '/student-audition',
							icon: 'star'
						},
						{
							id: '5',
							label: '跟进记录',
							path: '/student-follow',
							icon: 'attach'
						}
					]
				]
			};
		},
		onLoad() {
			const tabs = [{
				name: '全部学员',
				value: null,
				prop: 'status_all'
			}].concat(intentionStudentStatus);
			tabs.forEach((e) => {
				e.badge = {
					value: 0
				};
			});
			this.tabs = tabs;
			this.getList(1);
		},
		onReachBottom() {
			if (this.list.length < this.count) {
				this.getList();
			}
		},
		methods: {
			statusChange(e) {
				this.query.status = e.value;
				this.getList(1);
			},
			handleRouter(item, row) {
				this.$Router.push({
					path: item.path,
					query: {
						id: row.id
					}
				});
			},
			setTabsValue(ext) {
				this.tabs.forEach((e) => {
					e.badge.value = ext.student_sum?.[e.prop] || 0;
				});
			},
			getList(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				getStudent(query)
					.then((res) => {
						this.loading = false;
						this.query.page = query.page;
						const {
							list,
							count,
							ext
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							list.forEach((e) => {
								e.sexName = getSex(e.sex);
								e.statusData = getStudentStatus(e.status);
								e.contactRelationData = this.$getDict('contactRelationOptions', e
									.contact_relation);
							});
							this.list = this.list.concat(list);
						}
						if (this.query.page === 1) {
							this.setTabsValue(ext);
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>